package com.github.gin.sns.util;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class TuringTalk {
	public static String talk(String word){
		String resp = "";

		List<NameValuePair> params = new ArrayList<>();
		params.add(new BasicNameValuePair("key","83cf4a8acd91340bb70111be4a932fd4"));
		params.add(new BasicNameValuePair("info",word));

		URIBuilder builder = new URIBuilder().setScheme("http").setHost("www.tuling123.com").setPath("/openapi/api");
		builder.setParameters(params).setCharset(Charset.forName("UTF-8"));

		try {
			HttpGet get = new HttpGet(builder.build());
			get.setHeader("accept", "*/*");
			get.setHeader("Connection", "keep-alive");
			get.setHeader("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

			HttpResponse response = HttpClientBuilder.create().build().execute(get);
			resp = IOUtils.toString(response.getEntity().getContent(),Charset.forName("UTF-8"));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return resp;
	}

	public static void main(String[] args) {
		System.out.println(talk("机器人的图片"));
	}
}
